﻿
using MaterialDesignThemes.Wpf;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 基于WPF的电缆采样数据分析系统.Models;
using 基于WPF的电缆采样数据分析系统.Models.Dbs;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using 基于WPF的电缆采样数据分析系统.Service;
using 基于WPF的电缆采样数据分析系统.Service.Db;
using 基于WPF的电缆采样数据分析系统.Tools;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;
using 基于WPF的电缆采样数据分析系统.Tools.MessagePopupTool;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork
{
    public class EditImportWavformChannelUCViewModel : BindableBase, IDialogHostAware
    {
        /// <summary>
        /// 对话框主机唯一标识
        /// </summary>
        private const string DialogHostName = "RootDialog";

        #region 确定
        /// <summary>
        /// 确定命令
        /// </summary>
        public DelegateCommand SaveCommand { get; set; }

        /// <summary>
        /// 记录描述
        /// </summary>
        private string _RecordInfo;

        /// <summary>
        /// 记录描述
        /// </summary>
        public string RecordInfo
        {
            get { return _RecordInfo; }
            set { 
                _RecordInfo = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 确定方法
        /// </summary>
        private void Save()
        {
            // 判断导入波形内容是否正常
            if (string.IsNullOrEmpty(RecordInfo))
            {
                string info = LangugeConvertor.GetValue("RecordDescriptionCannotBeEmpty");
                MessagePopup.DisplayMessagePopup(info);
                return;
            }

            // 是否成功导入波形文件
            if (CurrentWavList == null || CurrentWavList.Count == 0)
            {
                string info = LangugeConvertor.GetValue("ImportingWaveformFileFailedProcedure");
                MessagePopup.DisplayMessagePopup(info);
                return;
            }

            // 保存通道波形
            switch (CurrentChannelValue)
            {
                case PassageType.ChannelOne:
                    App.ChannelDataList[0].WavDataType = CurrentDataValue;
                    App.ChannelDataList[0].PData = CurrentWavList;
                    break;
                case PassageType.ChannelTwo:
                    App.ChannelDataList[1].WavDataType = CurrentDataValue;
                    App.ChannelDataList[1].PData = CurrentWavList;
                    break;
                case PassageType.ChannelThree:
                    App.ChannelDataList[2].WavDataType = CurrentDataValue;
                    App.ChannelDataList[2].PData = CurrentWavList;
                    break;
            }

            // 缓存(待办)

            // 提交日志到数据库
            ImportWavLog importWavLog = new ImportWavLog() { RecordInfo = RecordInfo, 
                PassageType = (int?)CurrentChannelValue, 
                WavDataType = (int?)CurrentDataValue,
                CreateTime = DateTime.Now,
                FilePath = SelectedFilePath,
                CreateUser = GlobalVariableEdit.CurrentUserInfo.Uid
            };

            ImportWavLogService.Instance.SaveImportWavLog(importWavLog);

            var paras = new DialogParameters();
            paras.Add("ChannelType", CurrentChannelValue);
            paras.Add("FilePath", SelectedFilePath);

            // 关闭窗口
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, paras));
            }
        }

       

        #endregion

        #region 取消
        /// <summary>
        /// 取消命令
        /// </summary>
        public DelegateCommand CancelCommand { get; set; }

        /// <summary>
        /// 取消方法
        /// </summary>
        private void Cancel()
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
            }
        }

        #endregion

        public void OnDialogOpening(IDialogParameters parameters)
        {
            
        }


        #region 通道类型
        /// <summary>
        /// 通道改变命令
        /// </summary>
        public DelegateCommand<int?> ChannelChangedCmm { get; set; }

        /// <summary>
        /// 通道类型
        /// </summary>
        private List<TypeInfo> _ChannelTypeList;

        /// <summary>
        /// 通道类型
        /// </summary>
        public List<TypeInfo> ChannelTypeList
        {
            get { return _ChannelTypeList; }
            set
            {
                _ChannelTypeList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 当前通道值
        /// </summary>
        private PassageType _CurrentChannelValue = 0;

        /// <summary>
        /// 当前通道值
        /// </summary>
        public PassageType CurrentChannelValue
        {
            get { return _CurrentChannelValue; }
            set
            {
                _CurrentChannelValue = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 初始化通道类型数据
        /// </summary>
        private void InitChannelTypeList()
        {
            ChannelTypeList = new List<TypeInfo>();
            ChannelTypeList.Add(new TypeInfo() { ShowContent = "通道一", Value = 0 });
            ChannelTypeList.Add(new TypeInfo() { ShowContent = "通道二", Value = 1 });
            ChannelTypeList.Add(new TypeInfo() { ShowContent = "通道三", Value = 2 });
        }

        /// <summary>
        /// 通道改变方法
        /// </summary>
        /// <param name="selectedIndex"></param>
        private void ChannelChangedFun(int? selectedIndex)
        {
            CurrentChannelValue = (PassageType)(selectedIndex != null ? selectedIndex : 0);
        }
        #endregion

        #region 数据类型
        /// <summary>
        /// 数据类型改变命令
        /// </summary>
        public DelegateCommand<int?> DataTypeChangedCmm { get; set; }

        /// <summary>
        /// 数据类型
        /// </summary>
        private List<TypeInfo> _DataTypeList;

        /// <summary>
        /// 数据类型
        /// </summary>
        public List<TypeInfo> DataTypeList
        {
            get { return _DataTypeList; }
            set
            {
                _DataTypeList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 当前数据类型
        /// </summary>
        private WavDataType _CurrentDataValue = 0;

        /// <summary>
        /// 当前数据类型
        /// </summary>
        public WavDataType CurrentDataValue
        {
            get { return _CurrentDataValue; }
            set
            {
                _CurrentDataValue = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 初始化数据类型
        /// </summary>
        private void InitDataTypeList()
        {
            DataTypeList = new List<TypeInfo>();
            DataTypeList.Add(new TypeInfo() { ShowContent = "单条数据", Value = 0 });
            DataTypeList.Add(new TypeInfo() { ShowContent = "连续数据", Value = 1 });
        }

        /// <summary>
        /// 数据类型改变方法
        /// </summary>
        private void DataTypeChangedFun(int? selectedIndex)
        {
            CurrentDataValue = (WavDataType)(selectedIndex != null ? selectedIndex : 0);
        }

        #endregion

        /// <summary>
        /// 构造器
        /// </summary>
        public EditImportWavformChannelUCViewModel()
        {
            // 绑定事件改变命令
            ChannelChangedCmm = new DelegateCommand<int?>(ChannelChangedFun);
            DataTypeChangedCmm = new DelegateCommand<int?>(DataTypeChangedFun);
            SelectFileCmm = new DelegateCommand(SelectFileFun);
            // 取消
            CancelCommand = new DelegateCommand(Cancel);
            // 确定
            SaveCommand = new DelegateCommand(Save);

            // 初始化通道类型数据
            InitChannelTypeList();

            // 初始化数据类型
            InitDataTypeList();

            // 初始化读取波形按钮相关信息
            InitWavFileTypeInfo();
        }


        #region 读取文件
        /// <summary>
        /// 选择文件
        /// </summary>
        public DelegateCommand SelectFileCmm { get; set; }

        /// <summary>
        /// 当前波形数据
        /// </summary>
        private List<float> _CurrentWavList;

        /// <summary>
        /// 当前波形数据
        /// </summary>
        public List<float> CurrentWavList
        {
            get { return _CurrentWavList; }
            set { 
                _CurrentWavList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 是否选择文件信息集合
        /// </summary>
        private List<WavFileTypeInfo> _WavFileTypeInfoList;

        /// <summary>
        /// 是否选择文件信息集合
        /// </summary>
        public List<WavFileTypeInfo> WavFileTypeInfoList
        {
            get { return _WavFileTypeInfoList; }
            set { 
                _WavFileTypeInfoList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 按钮状态
        /// </summary>
        private string _ButtonStatus;

        /// <summary>
        /// 按钮状态
        /// </summary>
        public string ButtonStatus
        {
            get { return _ButtonStatus; }
            set { 
                _ButtonStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 图标状态
        /// </summary>
        private string _IconStatus;

        /// <summary>
        /// 图标状态
        /// </summary>
        public string IconStatus
        {
            get { return _IconStatus; }
            set {
                _IconStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 提示状态
        /// </summary>
        private string _HintStatus;

        /// <summary>
        /// 提示状态
        /// </summary>
        public string HintStatus
        {
            get { return _HintStatus; }
            set {
                _HintStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 选择的文件路径
        /// </summary>
        public string SelectedFilePath { get; set; }

        /// <summary>
        /// 初始化读取波形按钮相关信息
        /// </summary>
        private void InitWavFileTypeInfo()
        {
            WavFileTypeInfoList = new List<WavFileTypeInfo>();
            WavFileTypeInfoList.Add(new WavFileTypeInfo() { Wid=0, ButtonStatus= LangugeConvertor.GetValue("SelectFile"), IconStatus=LangugeConvertor.GetValue("Empty"), HintStatus= LangugeConvertor.GetValue("UnselectedFile") });
            WavFileTypeInfoList.Add(new WavFileTypeInfo() { Wid=1, ButtonStatus= LangugeConvertor.GetValue("ReselectFile"), IconStatus="OK", HintStatus= LangugeConvertor.GetValue("SelectedFile") });

            ButtonStatus = WavFileTypeInfoList[0].ButtonStatus;
            IconStatus = WavFileTypeInfoList[0].IconStatus;
            HintStatus = WavFileTypeInfoList[0].HintStatus;
        }

        /// <summary>
        /// 切换读取波形按钮相关信息
        /// </summary>
        /// <param name="isSelect"></param>
        private void ToggleWavFileTypeInfo(bool isSelect)
        {
            if (isSelect)
            {
                ButtonStatus = WavFileTypeInfoList[1].ButtonStatus;
                IconStatus = WavFileTypeInfoList[1].IconStatus;
                HintStatus = WavFileTypeInfoList[1].HintStatus;
            } else
            {
                ButtonStatus = WavFileTypeInfoList[0].ButtonStatus;
                IconStatus = WavFileTypeInfoList[0].IconStatus;
                HintStatus = WavFileTypeInfoList[0].HintStatus;
            }
        }


        /// <summary>
        /// 选择文件
        /// </summary>
        private void SelectFileFun()
        {
            try
            {
                // 创建一个OpenFileDialog的实例
                OpenFileDialog openFileDialog = new OpenFileDialog();

                // 设置文件选择器的标题
                openFileDialog.Title = LangugeConvertor.GetValue("SelectAWaveformFile");

                // 设置过滤器，只允许选择 .CSV 文件
                openFileDialog.Filter = $"CSV {LangugeConvertor.GetValue("File")} (*.csv)|*.csv";

                // 设置默认的文件类型筛选器索引
                openFileDialog.FilterIndex = 1;

                // 弹出文件选择器对话框，并检查用户是否点击了“确定”按钮
                if (openFileDialog.ShowDialog() == true)
                {
                    // 获取用户选择的文件路径
                    SelectedFilePath = openFileDialog.FileName;


                    CurrentWavList = TDRDataCsv.LoadWav(SelectedFilePath);

                    if (CurrentWavList.Count > 0)
                    {
                        ToggleWavFileTypeInfo(true);
                        //RealTimeOscilloscopeViewModel.Instance.ShowWavform(list);
                    }
                }
            }
            catch (Exception ex)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("CauseOfError") + ex.Message);
                return;
            }
        }
        #endregion
    }
}
